//
//  MYCollectionView.m
//  PetExchange
//
//  Created by a on 2021/3/7.
//

#import "MYCollectionView.h"



@interface MYCollectionView ()<DZNEmptyDataSetSource, DZNEmptyDataSetDelegate>
@property (nonatomic,strong) NSMutableArray *refreshImages;//刷新动画的图片数组
@property (nonatomic,strong) NSMutableArray *normalImages;//普通状态下的图片数组
@property (nonatomic , copy)  NSString  *tipText;
@property (nonatomic , strong)  UIImage  *tipImage;
@end
@implementation MYCollectionView

- (instancetype)initWithFrame:(CGRect)frame collectionViewLayout:(UICollectionViewLayout *)layout {
    self = [super initWithFrame:frame collectionViewLayout:layout];
    if (self) {
        [self setViewInfo];
    }
    return self;
}
- (void)setViewInfo {
    if (@available(iOS 11.0, *)) {
        self.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        self.contentInset = UIEdgeInsetsMake(NAVIH, 0, 49, 0);
        self.scrollIndicatorInsets = self.contentInset;
          }
    self.backgroundColor = [UIColor clearColor];
    self.emptyDataSetSource = self;
    self.emptyDataSetDelegate = self;
    self.noDataType = TableViewNoDataType_Data;
    self.tipText = LocalizedString(@"暂无数据");
    self.tipImage = [UIImage imageNamed:@"nodata_data"];
//    MJRefreshGifHeader *header = [MJRefreshGifHeader headerWithRefreshingTarget:self refreshingAction:@selector(tableViewRefreshData)];
//    [header setImages:self.refreshImages forState:MJRefreshStateRefreshing];
//    [header setImages:self.normalImages forState:MJRefreshStateIdle];
//    [header setImages:self.refreshImages forState:MJRefreshStatePulling];
//    header.lastUpdatedTimeLabel.hidden= YES;//如果不隐藏这个会默认 图片在最左边不是在中间
//    header.stateLabel.hidden = YES;
//    self.mj_header = header;
    
    [self addMJHeader];
    [self addMJFooter];
    
    self.showsVerticalScrollIndicator = NO;
}
- (void)setNoDataType:(NoDataType)noDataType {
    _noDataType = noDataType;
    if (_noDataType == TableViewNoDataType_NOLOGINFILE) {
        self.tipText = LocalizedString(@"你还没有登录，请登录后查看！");
        self.tipImage = [UIImage imageNamed:@"file_index_nodata"];
    }else if (_noDataType == TableViewNoDataType_FILE) {
        self.tipText = LocalizedString(@"暂时没有文件，添加吧~");
        self.tipImage = [UIImage imageNamed:@"file_index_nodata"];
    }else if (_noDataType == TableViewNoDataType_VIDEO) {
        self.tipText = LocalizedString(@"暂时没有视频，添加吧~");
        self.tipImage = [UIImage imageNamed:@"s_video_null"];
    }else if (_noDataType == TableViewNoDataType_AUDIO) {
        self.tipText = LocalizedString(@"暂时没有音频，添加吧~");
        self.tipImage = [UIImage imageNamed:@"s_audio_null"];
    }else if (_noDataType == TableViewNoDataType_Note) {
        self.tipText = LocalizedString(@"暂时没有笔记，添加吧~");
        self.tipImage = [UIImage imageNamed:@"s_note_null"];
    }else  if (_noDataType == TableViewNoDataType_Message) {
        self.tipText = LocalizedString(@"暂时没有加密短信哦~");
        self.tipImage = [UIImage imageNamed:@"m_index_null"];
    }else if (_noDataType == TableViewNoDataType_Data) {
        self.tipText = LocalizedString(@"暂无数据");
        self.tipImage = [UIImage imageNamed:@"nodata_data"];
    }
}

- (BOOL)emptyDataSetShouldAllowScroll:(UIScrollView *)scrollView{
    return YES;
}

- (UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView {

        return self.tipImage;
}
//- (UIImage *)buttonImageForEmptyDataSet:(UIScrollView *)scrollView forState:(UIControlState)state {
//    return [UIImage imageNamed:@"nodata_refresh"];
//}

- (void)emptyDataSet:(UIScrollView *)scrollView didTapButton:(UIButton *)button {
//    [self refreshDataWithTabelViewTag:0];
}

- (NSAttributedString *)titleForEmptyDataSet:(UIScrollView *)scrollView {
    NSString *title = self.tipText;
    NSDictionary *attributes = @{
                                 NSFontAttributeName:MY_SCALE_RegularFONT(14),
                                 NSForegroundColorAttributeName:MyColor(@"#ffffff")
                                 };
    return [[NSAttributedString alloc] initWithString:title attributes:attributes];
}

- (void)addMJHeader {
    self.mj_header = nil;
    MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(tableViewRefreshData)];
    self.mj_header = header;
}
- (void)addMJFooter {
    self.mj_footer = nil;
    MJRefreshBackNormalFooter*footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(tableViewLoadMoreData)];
    self.mj_footer = footer;
    [footer setTitle:LocalizedString(@"正在加载更多数据") forState:MJRefreshStateRefreshing];
}

- (void)tableViewRefreshData {
    [self.mj_header endRefreshing];
    if (self.refreshDataBlock) {
        self.refreshDataBlock();
    }
    
    if ([self.dataChangeDelegate respondsToSelector:@selector(refreshDataWithCollectViewTag:)]) {
        [self.dataChangeDelegate refreshDataWithCollectViewTag:self.tag];
    }
}

- (void)tableViewLoadMoreData {
     [self.mj_footer endRefreshing];
    if (self.loadMoreDataBlock) {
        self.loadMoreDataBlock();
    }
    if ([self.dataChangeDelegate respondsToSelector:@selector(loadMoreDataWithCollectViewTag:)]) {
        [self.dataChangeDelegate loadMoreDataWithCollectViewTag:self.tag];
    }
}


- (NSMutableArray *)refreshImages
{
    if (_refreshImages == nil) {
        _refreshImages = [[NSMutableArray alloc] init];
        //循环添加图片
        for (int i=0; i<13; i++) {
            //logoxu0
            UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"logo_%d", i]];
            
            [self.refreshImages addObject:image];
        }
    }
    return _refreshImages;
}
- (NSMutableArray *)normalImages
{
    if (_normalImages == nil) {
        _normalImages = [[NSMutableArray alloc] init];
        UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"logo_0.png"]];
        [self.normalImages addObject:image];
    }
    return _normalImages;
}


- (void)removeMJRefresh {
    self.mj_header = nil;
    self.mj_footer = nil;
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
